<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            var allPathsSourceTarget = function (graph) {
                let result = []
                let path = [0]
                //x表示当前遍历的节点  graph[i]表示从节点i能访问到的所有边
                function dfs(path, x) {
                    //终止条件
                    if (x == graph.length - 1) {
                        return result.push([...path])
                    }
                    for (let i = 0; i < graph[x].length; i++) {
                        //即当前节点可访问到的所有节点
                        path.push(graph[x][i])
                        dfs(path, graph[x][i]) //递归下一个节点
                        path.pop()
                    }
                }
                //跟二叉树一样，要把初始节点带上
                dfs(path, 0)
                return result
            }
        </script>
    </body>
</html>
